<?php
namespace app\controller;

use think\Request;
use think\facade\Db;
use app\model\Bazaar;
use app\model\Expand;

class FeedAccess extends Common
{

	/**
	 * 请求类型
	 *
	 * @return
	 */
	public function method(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'feedaccess:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
 
		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');
		if($sort == 'ipAddr'){
			$sort = 'ip';
		}

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );
		array_push( $where, ['userType', '=', 2 ] );

		$username = $request->param('username', '', 'trim');
		if($username) {
			array_push( $where, ['userid', '=',  $username ] );
		}

		$bazaarId = $request->param('bazaarId', '', 'trim');
		if($bazaarId) {
			array_push( $where, ['bazaarId', '=',  $bazaarId ] );
		}

		$accessPage = $request->param('accessPage', '', 'trim');
		if($accessPage) {
			array_push( $where, ['accessPage', '=',  $accessPage ] );
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			array_push( $where, ['accessTime', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, false) ] );
		} else {
			array_push( $where, ['accessTime', 'between', cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), false) ] );
		}
    //权限管理
		$roleTempId = Db::table('cfg_user')->where('id', $request->sessInfo['id'])->value('role');
		if($roleTempId) {
			$authaccount = Db::table('cfg_role')->where('companyId', $request->sessInfo['companyId'])->where('id', $roleTempId)->value('feedauthaccount');
			if($authaccount) {
				array_push( $where, ['userid', 'in',  $authaccount ] );
			}
		}
		$query = Db::name('cfg_access')->where($where)->order($sort, $order)->paginate($limit);

		$items = $query->items();
		$address = new \Net\IpLocation( app()->getRootPath() . 'data' . DIRECTORY_SEPARATOR . 'qqwry.dat');
		foreach ( $items as &$value ) {
			$value['addr'] = $address->getAddrName($value['ip']);
		}
		return ['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $items,
			'expand' => Expand::getList($request->sessInfo, 2),
			'bazaar' => Bazaar::getList($request->sessInfo, 2) ];
	}

	/**
	 * 清除三个月之前日志
	 *
	 * @return
	 */
	public function clear(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'feedaccess:clear')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$beginTime = microtime(true);
		$count = Db::table('cfg_access')->where('companyId', $request->sessInfo['companyId'])->where('userType', 2)->where('accessTime', '<=', date('Y-m-d H:i:s', time() - ( 86400 * 90 )))->count();
		Db::table('cfg_access')->where('companyId', $request->sessInfo['companyId'])->where('userType', 2)->where('accessTime', '<=', date('Y-m-d H:i:s', time() - ( 86400 * 90 )))->delete();
		$endTime = microtime(true);
		$this->cfg_write_oper($request, $request->sessInfo['token'], $request->sessInfo['companyId'], $request->sessInfo['nickname'],
		$request->sessInfo['username'], round($endTime - $beginTime, 3), '百度访问记录', '清除三个月之前日志!', 0, null);
		return json(['code'=> 0, 'msg'=> '成功删除'.$count.'条日志!']);
	}

}
